package dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.RainManageDao;
import entity.RainManage;

public class RainManageDaoImpl extends BaseDao implements RainManageDao {

	@Override
	public List<RainManage> getAllRain() {
		List<RainManage> list = new ArrayList<>();
		String sql = "select * from rainQuality order by monitorTime desc";
		try {
			pstm = getConn().prepareStatement(sql);
			rs = pstm.executeQuery();
			while (rs.next()) {
				RainManage rm = new RainManage(rs.getInt(1), rs.getString(2),
						rs.getDate(3), rs.getInt(4), rs.getString(5),
						rs.getString(6));
				list.add(rm);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(conn, pstm, rs);
		}
		return list;
	}

	@Override
	public int addRain(RainManage rm) {
		String sql = "insert into rainquality(districtName,monitorTime,rain,monitoringStation,monitoringAddress) values(?,?,?,?,?)";
		Object[] os = { rm.getDistrictName(), rm.getMonitorTime(),
				rm.getRain(), rm.getMonitoringStation(),
				rm.getMonitoringAddress() };
		return updateAll(sql, os);
	}

	@Override
	public int deleteRain(int id) {
		String sql = "delete from rainquality where id = ?";
		Object o = id;
		return updateAll(sql, o);
	}

}
